package com.ysw.rpc.client.utils;

import com.ysw.rpc.common.messages.Request;
import com.ysw.rpc.common.messages.Response;
import io.netty.util.concurrent.Promise;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author YinShangwen
 * @since 2023/4/18 11:26
 */
public class PromiseUtil {
    private static final Map<Long, Promise<Response>> PROMISE_MAP = new ConcurrentHashMap<>();

    /**
     * 从缓存中 删除并获取 一个promise
     *
     * @param requestId 请求Id-key
     * @return promise
     */
    public static Promise<Response> get(Long requestId) {
        return PROMISE_MAP.remove(requestId);
    }

    /**
     * 添加一个promise到缓存
     *
     * @param request request
     * @param promise promise
     */
    public static void put(Request request, Promise<Response> promise) {
        PROMISE_MAP.put(request.getId(), promise);
    }

}
